package com.ziq.zcamp.place

import android.graphics.Color
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.click
import com.ziq.base.extension.createBindingViewHolder
import com.ziq.base.extension.dp
import com.ziq.base.view.drawable.gradientDrawableBuild
import com.ziq.base.view.drawable.stateListDrawableBuild
import com.ziq.zcamp.bean.place.CampHotConditionItem
import com.ziq.zcamp.databinding.ItemFilterHotConditionBinding

class HotConditionAdapter : RecyclerView.Adapter<BindingViewHolder<ItemFilterHotConditionBinding>>(){

    var dataList: List<CampHotConditionItem>? = null

    var onSelectListener: ((categoryId:Int?, categoryType: Int?, conditionId:Int?, conditionName:String?)->Unit)? = null
    var onUnSelectListener: ((categoryId:Int?, categoryType: Int?, conditionId:Int?, conditionName:String?)->Unit)? = null

    fun setData(list: List<CampHotConditionItem>?){
        this.dataList = list
        notifyDataSetChanged()
    }

    private var selectedList : MutableMap<Int, MutableList<Int>>? = null
    fun updateSelectedCondition(selectedConditionList : MutableMap<Int, MutableList<Int>>?){
        this.selectedList = selectedConditionList
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return dataList?.size?:0
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemFilterHotConditionBinding> {
        return createBindingViewHolder(parent)
    }

    override fun onBindViewHolder(holder: BindingViewHolder<ItemFilterHotConditionBinding>, position: Int) {
        val item = dataList?.get(position)
        item?.let {
            holder.binding.name.text = item.name

            holder.binding.name.background = stateListDrawableBuild {
                drawableItem {
                    drawable = gradientDrawableBuild {
                        solidColor = Color.parseColor("#20211F")
                        cornerRadius = 10.dp.toFloat()
                    }
                    state = intArrayOf(android.R.attr.state_selected)
                }
                drawableItem {
                    drawable = gradientDrawableBuild {
                        solidColor = Color.parseColor("#F6F6F6")
                        cornerRadius = 10.dp.toFloat()
                    }
                    state = intArrayOf(-android.R.attr.state_selected)
                }
            }

            var selected = false
            if(selectedList?.containsKey(item?.parent_id?:-1) == true){
                if(selectedList?.get(item?.parent_id?:-1)?.contains(item?.id?:-1) == true){
                    selected = true
                }
            }
            holder.binding.name.isSelected = selected
            if(selected){
                holder.binding.name.setTextColor(Color.parseColor("#ffffff"))
            } else {
                holder.binding.name.setTextColor(Color.parseColor("#666666"))
            }


            holder.itemView.click {
                if(selected){
                    onUnSelectListener?.invoke(item?.parent_id, item?.parent_multi_select, item?.id, item?.name)
                } else {
                    onSelectListener?.invoke(item?.parent_id, item?.parent_multi_select, item?.id, item?.name)
                }
            }
        }
    }

}